package com.yixin.hospital.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.entity.domain.JobDutyMenu;
import com.yixin.hospital.entity.domain.Menu;
import com.yixin.hospital.entity.domain.UserMenu;
import com.yixin.hospital.mapper.MenuMapper;
import com.yixin.hospital.service.JobDutyMenuService;
import com.yixin.hospital.service.MenuService;
import com.yixin.hospital.service.UserMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author wb-caifenghua
* @description 针对表【menu(菜单表)】的数据库操作Service实现
* @createDate 2024-03-22 10:41:00
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{

    @Resource
    private UserMenuService userMenuService;
    @Resource
    private JobDutyMenuService jobDutyMenuService;


    @Override
    public ApiResult treeList() {
        List<Menu> list = this.list();
        TreeNodeConfig nodeConfig = new TreeNodeConfig();
        List<Tree<Integer>> treeList = TreeUtil.build(list, 0, nodeConfig,
                (node, tree) -> {
                    tree.setId(node.getId().intValue());
                    tree.setParentId(node.getPid().intValue());
                    tree.setName(node.getName());
                    tree.putExtra("label", node.getName());
                });
        return ApiResult.success(CollectionUtil.isEmpty(treeList) ? new ArrayList<>() : treeList);
    }

    @Override
    public ApiResult userMenus(Long userId) {
        // 查询用户所有菜单权限
        LambdaQueryWrapper<UserMenu> umQueryWrapper = new LambdaQueryWrapper<>();
        umQueryWrapper.eq(UserMenu::getUserId, userId);
        List<UserMenu> list = userMenuService.list(umQueryWrapper);
        return ApiResult.success(list.stream().map(userMenu -> userMenu.getMenuId()).collect(Collectors.toList()));
    }

    @Override
    public ApiResult userMenusByUserId(int userId) {
        // 查询用户所有菜单权限
        LambdaQueryWrapper<UserMenu> umQueryWrapper = new LambdaQueryWrapper<>();
        umQueryWrapper.eq(UserMenu::getUserId, userId);
        List<UserMenu> list = userMenuService.list(umQueryWrapper);
        return ApiResult.success(list.stream().map(userMenu -> userMenu.getMenuId()).collect(Collectors.toList()));
    }

    @Override
    public ApiResult jobDutyMenusByJobDutyId(int jobDutyId) {
        // 查询用户所有菜单权限
        LambdaQueryWrapper<JobDutyMenu> umQueryWrapper = new LambdaQueryWrapper<>();
        umQueryWrapper.eq(JobDutyMenu::getJobDutyId, jobDutyId);
        List<JobDutyMenu> list = jobDutyMenuService.list(umQueryWrapper);
        return ApiResult.success(list.stream().map(jobDutyMenu -> jobDutyMenu.getMenuId()).collect(Collectors.toList()));
    }
}




